<?php

namespace services\patient;

use Yii;
use common\components\Service;
use common\models\patient\OperateLog;
use common\models\member\Attr;

/**
 * Class OperateLogService
 * @package services\patient
 */
class OperateLogService extends Service
{
    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = OperateLog::find();

//        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);
//        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }

    /**
     * 操作日志列表
     *
     * @param array $params
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getOperateLogPage(array $params = [], int $page = 0, int $pageSize = 0)
    {
        $query = OperateLog::find()->select(['a.*', 'b.profession_name'])->alias('a')
            ->leftJoin(Attr::tableName().' AS b', 'b.member_id = a.operate_id AND b.project_id = '.$params['project_id']);

        if (isset($params['project_id'])) {
            $query->andWhere(['a.project_id' => $params['project_id']]);
        }

        if (isset($params['patient_user_id'])) {
            $query->andWhere(['a.patient_user_id' => $params['patient_user_id']]);
        }

        if (isset($params['action_type'])) {
            $query->andWhere(['a.action_type' => $params['action_type']]);
        }

        $query->orderBy([
            'a.created_at' => SORT_DESC,
            'a.id' => SORT_DESC,
        ]);

        // 总数
        $total = $query->count();

        // 分页
        if ($page && $pageSize) {
            $query->limit($pageSize)->offset(($page - 1) * $pageSize);
        }

        $list = $query->asArray()->all();

        return [
            'page' => (int)$page,
            'pageSize' => (int)$pageSize,
            'total' => (int)$total,
            'list' => $list,
        ];
    }

    /**
     * 格式化数据
     *
     * @param array $data
     * @return array
     */
    public function getOperateLogPageFormat(array $data = [])
    {
        $list = [];
        foreach ($data as $row) {
            // 患者回访，处理逻辑
            $callbackWay = '';
            $actionContent = $row['action_content'];
            if ($row['action_type'] == OperateLog::ACTION_TYPE_PATIENT_FOLLOW) {
                $contentArr = explode('|', $row['action_content']);
                $actionContent = $contentArr[0];
                $callbackWay = $contentArr[1] ?? '';
            }

            $list[] = [
                'id' => $row['id'],
                'project_id' => $row['project_id'],
                'patient_user_id' => $row['patient_user_id'],
                'action_type' => $row['action_type'],
                'action_type_str' => OperateLog::$actionTypes[$row['action_type']],
                'action_content' => $actionContent,
                'operate_name' => $row['operate_name'],
                'profession_name' => $row['profession_name'] ? $row['profession_name'] : '',
                'created_at_str' => $row['created_at'] ? date('Y-m-d H:i', $row['created_at']) : '',
                'callback_way_str' => $callbackWay,
            ];
        }

        return $list;
    }
}
